//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  methods: {//事件扩展
    getOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    onInit () {
      let distributionid = this.getOption('distributionid')
      let producttype = this.getOption('producttype')
      producttype.label = 'typeName'
      producttype.value = 'id'
      this.boxOptions.height = 500;
      this.getTypeProduct()
      let typeData = [
        { key: '0', value: "详情" },
        { key: '1', value: "列表" },
      ]
      this.columns.forEach(x => {
        if (x.field == "type") {
          x.bind = {
            key: "",
            data: typeData
          }
        }
      })
      let goods = this.getOption("goods");
      let type = this.getOption('type')
      type.data = typeData
      type.onChange = (v) => {
        if (v == 0) {
          goods.type = 'select'
        } else {
          goods.type = 'selectList'
        }
      }
      let a = '';
      let b = '';
      distributionid.onChange = (v) => {
        b = v
        this.getlist(a, v)
      }
      producttype.onChange = (v) => {
        if(v.length){
          a = v.join(',')
          this.getlist(v.join(','), b)
        }
      }
    },
    getTypeProduct(){
      let producttype = this.getOption('producttype')
      this.http
        .get("/api/producttypes/GetProductType")
        .then((res) => {
          producttype.data = res.data
        });
    },
    getlist (a, b, row) {
      this.editFormFields.goods = []
      let goods = this.getOption("goods");
      let parmas = {};
      if(this.currentAction == 'update'){
        parmas.typeid = a || (this.editFormFields.producttype ? this.editFormFields.producttype.split(',') : '')
        parmas.distributorld = b || this.editFormFields.distributionid
      }else{
        parmas.typeid = a
        parmas.distributorld = b
      }
      this.http
        .get("/api/producttypes/GetdistriGood", parmas)
        .then((res) => {
          if(res.data && res.data.length){
            goods.data = res.data.map((i) => {
              return {
                key: i.id,
                value: i.spare1,
              };
            });
          }else{
            goods.data = []
          }
          if(this.currentAction == 'update'){
           if(row.type == 1){
              goods.type = 'selectList'
              this.editFormFields.goods = row.goods.split(',')
            }else{
              goods.type = 'select'
              this.editFormFields.goods = row.goods
            }
          }
        });
    },
    rowClick ({ row, column, event }) {
      //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    modelOpenBefore (row) {
      let title = this.getOption('title')
      let type = this.getOption('type')
      if (this.currentAction == "Add") {
        title.readonly = false
        type.readonly = false
      } else {
        this.getlist(row.producttype, row.distributionid,row)
        title.readonly = true
        type.readonly = true
        let arr = row.producttype.split(',')
        this.editFormFields.producttype=[]
        arr.forEach(v=>{
          this.editFormFields.producttype.push(Number(v))
        })
      }
    },
  }
};
export default extension;